//
//  SEGActivityTaskDetailCell.m
//  Commom
//
//  Created by Clavis on 2020/7/24.
//

#import "SEGActivityTaskDetailCell.h"

@interface SEGActivityTaskDetailCell ()

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UIButton *actionButton;

@end

@implementation SEGActivityTaskDetailCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = UIColor.clearColor;
    self.containerView = ({
        UIView *containerView = [[UIView alloc] init];
        containerView.backgroundColor = kColor_W2;
        containerView.layer.shadowColor = [kColor_3 colorWithAlphaComponent:0.22].CGColor;
        containerView.layer.shadowOffset = CGSizeMake(0,0);
        containerView.layer.shadowOpacity = 1;
        containerView.layer.shadowRadius = 5;
        containerView.layer.cornerRadius = 5;
        [self.contentView addSubview:containerView];
        containerView;
    });
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    
    self.actionButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"去完成(1/3)" forState:UIControlStateNormal];
        [button setTitleColor:kColor_1 forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
        [button setTitle:@"已完成" forState:UIControlStateDisabled];
        [button setTitleColor:kColor_W forState:UIControlStateDisabled];
        [button setBackgroundImage:[UIImage imageWithColor:kColor_L2] forState:UIControlStateDisabled];
        button.titleLabel.font = kFont_11;
        button.userInteractionEnabled = NO;
        button.layer.cornerRadius = 14;
        button.layer.masksToBounds = YES;
        [self.containerView addSubview:button];
        button;
    });
    [self.actionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.size.mas_equalTo(CGSizeMake(84, 28));
        make.centerY.mas_equalTo(0);
    }];
    
    self.titleLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.text = @"邻里圈点赞";
        label.textColor = kColor_1;
        label.font = [UIFont boldSystemFontOfSize:17];
        [self.containerView addSubview:label];
        label;
    });
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.left.mas_equalTo(15);
        make.right.equalTo(self.actionButton.mas_left).mas_offset(-10);
    }];
    
    self.priceLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.text = @"10元京东券";
        label.textColor = kColor_T;
        label.font = kFont_12;
        [self.containerView addSubview:label];
        label;
    });
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.bottom.mas_equalTo(-15);
        make.right.equalTo(self.actionButton.mas_left).mas_offset(-10);
    }];
}

- (void)updateCellWithChildTaskModel:(SEGActivityChildTaskModel *)childTaskModel {
    self.titleLabel.text = childTaskModel.childTaskName;
    self.priceLabel.text = childTaskModel.prizeName;
    
    if (childTaskModel.whetherComplete == 1) {
        self.actionButton.enabled = NO;
    } else {
        self.actionButton.enabled = YES;
        NSString *actionSting = @"去完成";
        // 子任务是多次任务时 & 完成的操作次数不为0 展示:完成的操作次数/总的操作次数
        if (childTaskModel.operateNum > 1 && childTaskModel.completeOperateCount >= 1) {
            actionSting =  [NSString stringWithFormat:@"去完成(%ld/%ld)", childTaskModel.completeOperateCount, childTaskModel.operateNum];
        }
        [self.actionButton setTitle:actionSting forState:UIControlStateNormal];
    }
}

#pragma mark - UITraitEnvironment
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    self.containerView.layer.shadowColor = [kColor_3 colorWithAlphaComponent:0.22].CGColor;
    [self.actionButton setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
    [self.actionButton setBackgroundImage:[UIImage imageWithColor:kColor_L2] forState:UIControlStateDisabled];
}

@end
